/**
 * 主要包含了登录相关的状态管理
 */
import Vue from 'vue'
import types from '../types/TypesMutation'
import localTypes from '../types/TypesLStorage'


// state 状态定义 : 全局变量/参数定义
const state = {
  //用户信息
  userInfo: {},
  //图片地址的前缀
  imgPrefix: "",
  //常用字典
  //loginTypeDict 订单状态
  // genderDict  性别
  // priceUnits   计价单位
  // orderStateDict   订单状态
  // packageStateDict  包裹状态
  // orderItemStateDict 订单子项状态
  // payTypeDict  支付方式
  // deliverCompanyList  快递公司
  dictionaries: {},
  isAutoLogin:true,
  //当前是否需要申请指纹验证
  isNeedFinger:false,
  product:{
    classifications:[],
    allTabs:[],
    allSysSize:{},
    allManageTabs: [],
  },
  webDomain:'',
  siteEmail:'',
  sendName:'',
  order:{
    orderStateEnum:[],
    userStateEnum:[],
    shipStateEnum:[],
  },
};

// mutations 修改 state的唯一途径. 注意,这里都是同步修改. 耗时操作请使用 action
const mutations = {

  [types.user.storeUserInfo](state, userInfo){
    // log(userInfo,"storeUserInfo-- ");
    state.userInfo = userInfo;
  },

  [types.base.storeImgPrefix](state, yunPath){
    state.imgPrefix = yunPath;
  },

  [types.user.clearUserInfo](state){
    state.userInfo = {};
  },

  [types.user.storeDics](state, dics){
    state.dictionaries = dics;
  },
  [types.user.autoLogin](state, isAutoLogin){
    state.isAutoLogin = isAutoLogin;
  },
  [types.local.storeBrowerFinger](state, browerFinger){
    state.browerFinger = browerFinger;
  },
  [types.local.storeIsNeedFinger](state, isNeedFinger){
    state.isNeedFinger = isNeedFinger;
  },
  [types.product.storeClassifications](state, classifications){
    state.product.classifications = classifications;
  },
  [types.product.storeTabs](state, allTabs){
    state.product.allTabs = allTabs;
  },
  [types.product.storeSysSize](state, allSysSize){
    state.product.allSysSize = allSysSize;
  },
  [types.product.storeManageTabs](state, allManageTabs){
    state.product.allManageTabs = allManageTabs;
  },
  [types.base.webDomain](state, domain){
    state.webDomain = domain;
  },
  [types.base.siteEmail](state, siteEmail){
    state.siteEmail = siteEmail;
  },
  [types.base.sendName](state, sendName){
    state.sendName = sendName;
  },
  [types.order.orderStateEnum](state, orderStateEnum){
    state.order.orderStateEnum = orderStateEnum;
  },
  [types.order.userStateEnum](state, userStateEnum){
    state.order.userStateEnum = userStateEnum;
  },
  [types.order.shipStateEnum](state, shipStateEnum){
    state.order.shipStateEnum = shipStateEnum;
  },
  [types.base.aliImgPrefix](state, aliImgPrefix){
    state.aliImgPrefix = aliImgPrefix;
  },
  [types.base.siteInfo](state, siteInfo){
    state.siteInfo = siteInfo;
  },
};



const actions = {

  queryDictionaries({commit}){
    Vue.prototype.$apiMain.getDictionaries().then(function (response) {
      if (response.success) {
        commit(types.user.storeDics,response.result);
      }
    });
  },
};


const getters = {
  // 用户信息
  userInfo: state => {
    return state.userInfo;
  },
  //是否为 admin 用户
  isAdmin: state => {
    return state.userInfo.access === 0;
  },
  //突破前缀
  imgPrefix: state => {
    return state.imgPrefix;
  },
  isAutoLogin:state => {
    return state.isAutoLogin;
  },
  browerFinger:state => {
    return state.browerFinger;
  },
  isNeedFinger:state => {
    return state.isNeedFinger;
  },
  dictionaries: state => {
    //loginTypeDict 订单状态
    // genderDict  性别
    // priceUnits   计价单位
    // orderStateDict   订单状态
    // packageStateDict  包裹状态
    // orderItemStateDict 订单子项状态
    // payTypeDict  支付方式
    // stateDict
    // deliverCompanyList  快递公司
    // targetCustomerStateDict  instagram 抓取客户信息状态
    // customerOperation  操作instagram 抓取客户信息的记录
    //pushPlatformDict  推送类型
    //messageTypeDict   消息推送类型
    // siteCodeDict  站点列表
    return state.dictionaries;
  },
  product:state=>{
    return state.product;
  },
  webDomain:state=>{
    return state.webDomain;
  },
  siteEmail:state=>{
    return state.siteEmail;
  },
  sendName:state=>{
    return state.sendName;
  },
  order:state=>{
    return state.order;
  },
  aliImgPrefix:state=>{
    return state.aliImgPrefix;
  },
  siteInfo:state=>{
    return state.siteInfo;
  },
};

function log(obj, tag) {
  if (null === tag) {
    tag = '';
  }
  let des = "";
  for (var name in obj) {
    des += name + ":" + obj[name] + ";";
  }
}



export default {
  state,
  mutations,
  actions,
  getters
}


